#include     <sys/socket.h>
#include     <netinet/in.h>
#include     <arpa/inet.h>
#include     <stdio.h>
#include     <stdlib.h>
#include     <string.h>
#define      LINE_SIZE 100
int main(int argc, char **argv)
{

    int     sockfd;
    struct sockaddr_in servaddr,replyaddr;
    char    line[LINE_SIZE+1]="time";

    if(argc != 2)
        printf("usage: udpcli <IPaddress>");

    memset(&servaddr, 0,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(13);
    if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr)<=0){
        printf("inet error!\n");
        return 1;
    }
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    socklen_t servlen=sizeof(servaddr);
    printf("Client OK!\n");
    sendto(sockfd,line, strlen(line), 0, (struct sockaddr *)&servaddr, servlen);
    memset(&line, 0,LINE_SIZE);

    socklen_t len=servlen;
    recvfrom(sockfd,line, LINE_SIZE, 0, (struct sockaddr *)&replyaddr, &len);
    if (len != servlen || memcmp(&servaddr, &replyaddr, len) != 0)
    {
        printf("reply from %s (ignored)\n", inet_ntoa(replyaddr.sin_addr));
        return 1;

    }
    else
    {

        line[LINE_SIZE]='\0';
        printf("The time is:%s",line);
    }

    getchar();
    return 0;
}

